
//PLAYLIST.JS
//shows the songs of a playlist
// Author: Wilco

function playlistPage() {}
playlistPage.prototype.constructor = playlistPage;
playlistPage.prototype =
{
    templateData: '',
    playlistJSON: '',
	playlistData: {},
	hooksData: [],
	listTmpl: null,
	searchInput: null,
    loadPage: function()
    {
        this.onOutput = function(title,pageTemplate) { ProcessPageOutput(title,pageTemplate,'',true); }
        this.loadDefaults();
        //Load template data
        LoadFile(SkinDir + 'playlist.html', this.onLoadTemplate.bind(this));
    },
    loadDefaults: function()
    {  
       //No Defaults
    },
    onLoadTemplate: function(response)
    {
        this.templateData = response;
        this.loadData();
    },
    loadData: function()
    {
		if (!gPageData['playlist']['guid'])
		{
			showError('No Playlist ID');
			return;
		}
        //Load playlists data
        var url = ServiceDir + 'playlist/' + gPageData['playlist']['guid']; 
        LoadFile(url, this.onLoadData.bind(this));
		
		
    },
	onLoadHooks: function(response)
	{
		this.hooksData = eval("(" + response + ")");
		var style = (this.hooksData.length > 0) ? 'block' : 'none';
		
		$('.menuDetailsButton').each(function(el) { 
		this.style.display = style; 
		});
	},
    onLoadData: function(response)
    {
       this.playlistJSON = response;
       this.generateOutput();
	   
		//Load hooks data
		var url = ServiceDir + 'hooks/mediaItem'; 
        LoadFile(url, this.onLoadHooks.bind(this));
    },
    generateOutput: function()
    {
		
        try
        {    this.playlistData = eval("(" + this.playlistJSON + ")"); }
        catch (e)
        {    showError('Cannot understand service data'); return; }        

		
		//set indexes before we sort!
		for (var i=0;i<this.playlistData.tracks.length;i++)
		{
			this.playlistData.tracks[i].index = i;
		}
		
		var tmpl = new Template();
		tmpl.load(this.templateData);
		this.listTmpl = tmpl.removeSubTemplate('list');
		
		
		this.onOutput(this.playlistData['name'],tmpl);
		
		//Hook events
		$('#SearchInput').bind('keyup',SearchUpdate);
		
		var tracks = this.playlistData.tracks;
		this.updateList(tracks);
	},
	updateList: function(tracks)
	{
		var tmplData = { item: [] };
		for(var i=0;i<tracks.length;i++)
		{
			tmplData.item.push({ name: tracks[i].name,
								 artist: tracks[i].artist,
								 album: tracks[i].album,
									guid: tracks[i].guid,
									index: tracks[i].index});
		}
		
		var tmpl = this.listTmpl.clone();
		tmpl.setData(tmplData);
		tmpl.addData({ 'playlistguid': this.playlistData.guid });
		
		document.getElementById('pagelist').innerHTML = tmpl.parse();
    },
	runSearch: function(searchStr)
	{
		var tracks = this.playlistData.tracks;
		tracks = LocalSearch(tracks,['name','artist'],searchStr);
		this.updateList(tracks);
	},
    onOutput: function(){}
}

function SearchUpdate(eventObj)
{
	var str = $('#SearchInput').val();
	gCurrentPage.runSearch(str);
}

function PlaylistItemButton_Click(el,i)
{
	CreateMediaItemMenu(gCurrentPage.hooksData,gCurrentPage.playlistData.tracks[i]);
	ShowMenu();
}

gLastScriptLoaded = new playlistPage();
gScriptLoaded();